﻿using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using A.M.Storage.Infrastruecure;
using O.M.Production.Application.IService;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using O.M.Production.Infrastuecure.IRepository;
using O.M.Production.Infrastuecure.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace O.M.Production.Application.Service
{
    /// <summary>
    /// 工序分类服务接口实现类
    /// </summary>
    public class working_procedure_type_Service : BaseServices<working_procedure_type, string>, I_working_procedure_type_Service
    {
        private readonly I_working_procedure_type_Repository _working_Procedure_Type_Repository;
        public working_procedure_type_Service(I_working_procedure_type_Repository working_Procedure_Type_Repository) : base(working_Procedure_Type_Repository)
        {
            _working_Procedure_Type_Repository = working_Procedure_Type_Repository;
        }
        /// <summary>
        /// 分页查询-工序分类
        /// </summary>
        /// <param name="WPTId_Name">工序分类编号或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格</returns>
        public async Task<ApiResult> Get_working_procedure_type(string? WPTId_Name, int pageSize, int pageIndex)
        {
            try
            {
                ApiResult apiResult = new ApiResult();
                Expression<Func<working_procedure_type, bool>> whereidname = i => true;
                if (!string.IsNullOrEmpty(WPTId_Name))
                {
                    whereidname = i => i.working_procedure_id.Contains(WPTId_Name) || i.working_procedure_name.Contains(WPTId_Name);
                }
                var list = await _working_Procedure_Type_Repository.GetShowInforAsync(pageSize, pageIndex, whereidname);
                apiResult.totalCount = list.TotalCount;
                apiResult.pageCount = list.PageCount;
                apiResult.Data = list.Data.OrderByDescending(x => x._add_time).ToList();
                return apiResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
